from django.db import models
from django.contrib.auth.models import User

class AssetType(models.Model):
    name = models.CharField(max_length=50)
    resource = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.resource
    
    class Meta:
        db_table = 'AssetTypes'


class Asset(models.Model):
    media = models.CharField(max_length=100)
    credit = models.CharField(max_length=200, null="True")
    caption = models.CharField(max_length=200, null="True")
    asset_type = models.ForeignKey(AssetType)
    creator = models.ForeignKey(User)
    creation_date = models.DateTimeField()
    approved = models.BooleanField()
    
    def getValue(self, key):
        if key == "media":
            return self.media
        elif key == "credit":
            return self.credit
        elif key == "caption":
            return self.caption
        else:
            return None

    def toDict(self, values):
        asset = {}
        
        for i in values:
            if self.getValue(i) != None:
                asset[i] = self.getValue(i)
                
        return asset
    
    def __str__(self):
        return "%s : %s" % (self.asset_type, self.media)

    class Meta:
        db_table = 'Assets'

    
class Event(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=1000, null="True")
    creator = models.ForeignKey(User)
    date = models.DateTimeField()
    creation_date = models.DateTimeField()
    approved = models.BooleanField()
    
    assets = models.ManyToManyField(Asset, 
                                       db_table="EventAssets")
    
    def getValue(self, key):
        if key == "title" or key == "headline":
            return self.title
        elif key == "description" or key == "text":
            return self.description
        elif key == "date" or key == "startDate":
            return self.date.strftime("%Y,%m,%d")
        else:
            return None
        
    def toDict(self, values):
        event = {}
        
        for i in values:
            if self.getValue(i) != None:
                event[i] = self.getValue(i)
                
        return event
    
    def __unicode__(self):
        return self.title

    def __str__(self):
        return "%s, %s" % (self.title, self.creator.first_name)

    class Meta:
        db_table = 'Events'
        